package com.yanqu.road.server.gameplayer.module.activity.advertisement;


import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.UserActivityConditionData;
import com.yanqu.road.entity.activity.advertisement.AdvertisementSystemData;
import com.yanqu.road.entity.activity.advertisement.AdvertisementUserData;
import com.yanqu.road.entity.activity.advertisement.config.AdvertisementConfig;
import com.yanqu.road.entity.activity.advertisement.config.AdvertisementSystemConfig;
import com.yanqu.road.entity.activity.luckyshop.LuckyShopConfig;
import com.yanqu.road.entity.activity.luckyshop.config.LuckyShopDrawConfig;
import com.yanqu.road.entity.activity.washe.config.WasheConfig;
import com.yanqu.road.entity.config.goods.BuyGoodsResult;
import com.yanqu.road.entity.config.goods.MallInfo;
import com.yanqu.road.entity.enums.*;
import com.yanqu.road.entity.enums.activity.advertisement.eAdvertisementSystemId;
import com.yanqu.road.entity.log.LogLuckyShopDraw;
import com.yanqu.road.entity.log.LogWatchAdvertisement;
import com.yanqu.road.entity.player.UserTravel;
import com.yanqu.road.entity.travel.TravelResult;
import com.yanqu.road.exception.BusinessException;
import com.yanqu.road.logic.bussiness.activity.AdvertisementActivityBusiness;
import com.yanqu.road.logic.config.ChannelConfig;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.logic.pb.TravelPb;
import com.yanqu.road.pb.activity.AdvertisementProto;
import com.yanqu.road.pb.activity.DaoMuProto;
import com.yanqu.road.pb.activity.RecoveryRewardProto;
import com.yanqu.road.pb.assistant.AssistantProto;
import com.yanqu.road.pb.travel.TravelProto;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.ActivityMallModule;
import com.yanqu.road.server.gameplayer.module.activity.ActivityModule;
import com.yanqu.road.server.gameplayer.module.activity.daomu.DaoMuActivityModule;
import com.yanqu.road.server.gameplayer.module.activity.luckyshop.LuckyShopModule;
import com.yanqu.road.server.gameplayer.module.base.GeneralModule;
import com.yanqu.road.server.gameplayer.module.kowloon.KowloonModule;
import com.yanqu.road.server.gameplayer.module.player.CardModule;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.gameplayer.module.player.EarnSpeedModule;
import com.yanqu.road.server.gameplayer.module.player.PlayerModule;
import com.yanqu.road.server.gameplayer.module.specialcookboy.SpecialCookBoyModule;
import com.yanqu.road.server.gameplayer.module.travel.TravelModule;
import com.yanqu.road.server.gameplayer.module.union.cross.CrossUnionModule;
import com.yanqu.road.server.logic.activity.BaseActivityData;
import com.yanqu.road.server.logic.activity.condition.BaseActivityCondition;
import com.yanqu.road.server.logic.mail.MailManager;
import com.yanqu.road.server.manger.MultipleLanguageMgr;
import com.yanqu.road.server.manger.ServerListMgr;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.MallActivityMgr;
import com.yanqu.road.server.manger.activity.advertisement.AdvertisementActivityMgr;
import com.yanqu.road.server.manger.activity.luckyshop.LuckyShopMgr;
import com.yanqu.road.server.manger.activity.washe.WasheMgr;
import com.yanqu.road.server.manger.config.SystemOpenMgr;
import com.yanqu.road.server.manger.config.TravelMgr;
import com.yanqu.road.server.manger.log.AutoLogMgr;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.utils.ConfigHelper;
import com.yanqu.road.utils.LocalDateTimeHelper;
import com.yanqu.road.utils.date.DateHelper;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class AdvertisementActivityModule extends GeneralModule {

    private AdvertisementUserData userData;

    private int nowWatchAd;

    private long nowWatchAdOpenTime;

    public AdvertisementActivityModule(GamePlayer player) {
        super(player);
    }

    @Override
    public boolean loadData() {
        ActivityInfo activityInfo = AdvertisementActivityMgr.getActivityInfo();
        if (activityInfo == null){
            return true;
        }
        userData = AdvertisementActivityBusiness.getAdvertisementUserData(activityInfo.getActivityId(),player.getUserId());
        if (userData == null){
            initUserData(activityInfo.getActivityId());
        }
        return true;
    }

    @Override
    public boolean afterLoadData() {
        resetWatchingAdState();
        return true;
    }

    @Override
    public boolean saveData() {
        if (userData != null){
            if (userData.isInsertOption()){
                AdvertisementActivityBusiness.addAdvertisementUserData(userData);
            }else if (userData.isUpdateOption()){
                AdvertisementActivityBusiness.updateAdvertisementUserData(userData);
            }
        }
        return true;
    }

    @Override
    public void loginSendMsg() {
        syncConfig();
        checkAll();
        syncData();
    }

    /**
     * 检测需要刷新的东西 每日次数 npc商店等
     */

    public void checkAll(){
        if (AdvertisementActivityMgr.getActivityInfo() != null){
            dailyReset();
            //checkRefreshNpcMall();
        }
    }

    @Override
    public void afterLogin() {
        checkConditionAutoSend();
    }

    public AdvertisementUserData getUserData() {
        return userData;
    }

    public void syncConfig(){
        if (AdvertisementActivityMgr.getActivityInfo() == null){
            return;
        }
        AdvertisementProto.AdvertisementConfigSyncMsg.Builder configBuilder = AdvertisementActivityMgr.getConfigBuilder(player.getLanguage());
        player.sendPacket(ClientProtocol.U_ADVERTISEMENT_ACTIVITY_SYNC_CONFIG,configBuilder);
    }

    /**
     * 小玉领取广告的成就奖励
     */
    public void getConditionReward(AssistantProto.AssistantActionRespMsg.Builder builder){
        ActivityInfo activityInfo = AdvertisementActivityMgr.getActivityInfo();
        if (activityInfo == null){
            return;
        }
        if (userData == null){
            return;
        }
        if (!SystemOpenMgr.systemOpen(player,eSystemId.Advertisement.getValue())){
            return;
        }

        BaseActivityData baseActivityData = player.getModule(ActivityModule.class).getBaseActivityData(activityInfo.getActivityId());
        if (baseActivityData == null){
            return;
        }
        Property totalReward = new Property();

        List<UserActivityConditionData> activityConditionData = baseActivityData.getUserActivityConditionDataList();
        for(UserActivityConditionData tempData : activityConditionData){
            //只检测每日看广告的活动
            if (tempData.getType() != eGamePlayerEventType.AdvertisementDailyResetCondition.getValue()){
                continue;
            }
            //首先 要能完成
            if(baseActivityData.canCompleted(tempData.getConditionId())){
                BaseActivityCondition baseActivityCondition = baseActivityData.getBaseActivityCondition(tempData.getConditionId());
                //其次 要没完成过
                if (!baseActivityCondition.isGetReward()) {
                    String reward = baseActivityCondition.getReward();
                    if (!StringUtils.isNullOrEmpty(reward)){
                        Property property = PropertyHelper.parseStringToProperty(reward);
                        totalReward.addProperty(property);
                    }
                }
            }
        }
        if (!totalReward.isNothing()){
            String reward = PropertyHelper.parsePropertyToString(totalReward);
            builder.setAdConditionReward(reward);
        }
    }


    /**
     * 自动发奖 限时奖励那个
     */

    public void checkConditionAutoSend(){
        ActivityInfo activityInfo = AdvertisementActivityMgr.getActivityInfo();
        if (activityInfo == null){
            return;
        }
        if (userData == null){
            return;
        }
        if (!SystemOpenMgr.systemOpen(player,eSystemId.Advertisement.getValue())){
            return;
        }
        int activeDay = AdvertisementActivityMgr.getConfig().getTotalWatchRewardActiveDay();
        if (userData.getActiveTime() == 0 || userData.getActiveTime() + activeDay * DateHelper.DAY_MILLIONS > System.currentTimeMillis()){
            return;
        }

        List<Integer> conditionList= new ArrayList<>();
        BaseActivityData baseActivityData = player.getModule(ActivityModule.class).getBaseActivityData(activityInfo.getActivityId());
        Property totalReward = new Property();
        if(baseActivityData != null){
            List<UserActivityConditionData> activityConditionData = baseActivityData.getUserActivityConditionDataList();
            for(UserActivityConditionData tempData : activityConditionData){
                //只检测限时看广告的活动 另外的不是在这补发
                if (tempData.getType() != eGamePlayerEventType.TotalWatchAdvertisementTimeCondition.getValue()){
                    continue;
                }

                if(baseActivityData.canCompleted(tempData.getConditionId())){
                    BaseActivityCondition baseActivityCondition = baseActivityData.getBaseActivityCondition(tempData.getConditionId());
                    if (!baseActivityCondition.isGetReward()) {
                        //奖励到手
                        totalReward.addProperty(PropertyHelper.parseStringToProperty(baseActivityCondition.getActivityConditionInfo().getRewardStr()));
                        baseActivityData.finish(tempData.getConditionId());
                        tempData.setGetReward(true);
                        conditionList.add(tempData.getConditionId());
                    }
                }
            }
        }
        //有奖励
        if (conditionList.size() > 0){
            if (!totalReward.isNothing()){
                //{0}活动进度未领取奖励发放
                //亲爱的掌柜，您在 {0}活动中还有未领取的进度奖励，现已补发，敬请查收。
                String mailTitle = StringUtils.format(MultipleLanguageMgr.getContent(MailManager.TREASURE_ACTIVITY_PROGRESS_REWORD_TITLE, player.getLanguage()), activityInfo.getActivityName());
                String mailContent = StringUtils.format(MultipleLanguageMgr.getContent(MailManager.TREASURE_ACTIVITY_PROGRESS_REWORD_CONTENT,  player.getLanguage()),
                        activityInfo.getActivityName());
                MailManager.sendMail(player.getUserId(), eMailType.SYSTEM.getValue(), PropertyHelper.parsePropertyToString(totalReward), mailContent, mailTitle);
            }
        }

    }

    public void resetWatchingAdState(){
        nowWatchAd = 0;
        nowWatchAdOpenTime = 0;
    }

    private void initUserData(int activityId){
        //不为空且是这个活动的数据
        if (userData != null && userData.getActivityId() ==activityId){
            return;
        }
        //初始化数据
        Map<Integer, AdvertisementSystemData> initAdvertisementSystemDataMap = AdvertisementActivityMgr.getInitAdvertisementSystemDataMap();
        if (initAdvertisementSystemDataMap == null){
            log.error("广告活动系统数据配置不存在,活动id{}",activityId);
            return;
        }
        Map<Integer, Integer> totalWatchMap = AdvertisementActivityMgr.getInitTotalWatchMap();
        if (totalWatchMap == null){
            log.error("广告活动总观看次数配置不存在,活动id{}",activityId);
            return;
        }
        AdvertisementUserData tmpUserData = new AdvertisementUserData();
        tmpUserData.setActivityId(activityId);
        tmpUserData.setUserId(getUserId());
        //初始化时间 即累计活动开始时间
        tmpUserData.setActiveTime(System.currentTimeMillis());
        tmpUserData.setSystemDataMap(initAdvertisementSystemDataMap);
        tmpUserData.setTotalWatchData(totalWatchMap);

        tmpUserData.setEarnSpeedBuffRefreshTime(0);
        tmpUserData.setEarnSpeedBuffCount(0);
        tmpUserData.setNpcDailyStage(0);
        tmpUserData.setNpcLastRefreshTime(0);
        tmpUserData.setNpcReward(AdvertisementActivityMgr.randomNpcReward());

        tmpUserData.setInsertOption();
        userData = tmpUserData;

    }

    public void syncData(){
        if (AdvertisementActivityMgr.getActivityInfo() == null){
            return;
        }
        int activityId = AdvertisementActivityMgr.getActivityInfo().getActivityId();
        if (userData == null || userData.getActivityId() != activityId){
            initUserData(activityId);
        }
        if (userData == null){
            log.error("广告活动玩家数据生成失败,玩家id{}",player.getUserId());
            return;
        }
        AdvertisementProto.AdvertisementUserDataSyncMsg.Builder dataBuilder = AdvertisementProto.AdvertisementUserDataSyncMsg.newBuilder();
        dataBuilder.setUserdata(parseUserDataToMsg(userData));
        player.sendPacket(ClientProtocol.U_ADVERTISEMENT_ACTIVITY_SYNC_USER_DATA,dataBuilder);
    }

    /**
     * 解析玩家数据到消息
     * @param userData
     * @return
     */

    public AdvertisementProto.AdvertisementUserData.Builder parseUserDataToMsg(AdvertisementUserData userData){
        AdvertisementProto.AdvertisementUserData.Builder builder = AdvertisementProto.AdvertisementUserData.newBuilder();
        builder.setActiveTime(userData.getActiveTime());
        builder.setLastGetEarnSpeedAddBuffTime(userData.getEarnSpeedBuffRefreshTime());
        builder.setHasEarnSpeedAddBuff(userData.getEarnSpeedBuffCount());
        builder.setNowNpcReward(userData.getNpcReward());
        builder.setLastNpcRefreshTime(userData.getNpcLastRefreshTime());
        for (AdvertisementSystemData advertisementSystemData:userData.getSystemDataMap().values()){
            AdvertisementProto.AdvertisementUserSystemData.Builder systemBuilder = AdvertisementProto.AdvertisementUserSystemData.newBuilder();
            systemBuilder.setSystemId(advertisementSystemData.getSystemId());
            systemBuilder.setHasWatchTimes(advertisementSystemData.getHasWatchTimes());
            systemBuilder.setLastRefreshLimitTime(advertisementSystemData.getLastRefreshLimitTime());
            systemBuilder.setLastWatchTime(advertisementSystemData.getLastWatchTime());
            builder.addSystemData(systemBuilder);
        }
        return builder;
    }

    /**
     * 检查每日重置次数
     */
    public void dailyReset(){
        if(null == userData){
            return;
        }
        // 没有活动
        if (!ActivityMgr.activityInTime(AdvertisementActivityMgr.getActivityInfo()) || AdvertisementActivityMgr.getConfig() == null) {
            return;
        }
        long zeroTime = LocalDateTimeHelper.getZeroTimeTimeStamp();
        boolean needSync = false;
        if (userData.getSystemDataMap() == null){
            return;
        }
        for (AdvertisementSystemData data:userData.getSystemDataMap().values()){
            // 6.0优化 npc小人返回通用的刷新机制
//            if (data.getSystemId() == eAdvertisementSystemId.buildingNpc.getValue()){
//                continue;
//            }
            if (data.getLastRefreshLimitTime() < zeroTime) {
                if (data.getHasWatchTimes() > 0) {
                    needSync = true;
                }
                data.setHasWatchTimes(0);
                data.setLastRefreshLimitTime(zeroTime);
            }
        }
        if(needSync){
            userData.setUpdateOption();
            syncData();
        }
    }

    /**
     * 开始看广告
     * @param activityId
     * @param systemId
     * @param startTime
     * @return
     */

    public int watchAdStart(int activityId,int systemId,long startTime){
        int simpleCheck = simpleCheck(activityId);
        if (simpleCheck != 0){
            return simpleCheck;
        }
        //先刷新观看状态
        resetWatchingAdState();
        //检测每日次数刷新 检测npc小人
        checkAll();
        //拿出系统数据
        if (!userData.getSystemDataMap().containsKey(systemId)){        //如果不存在
            //先去拿总配置 看看有没有这个系统
            Map<Integer, AdvertisementSystemData> initAdvertisementSystemDataMap = AdvertisementActivityMgr.getInitAdvertisementSystemDataMap();
            if (initAdvertisementSystemDataMap == null){
                log.error("广告活动系统数据配置不存在,活动id{}",activityId);
                return GameErrorCode.E_ADVERTISEMENT_ACTIVITY_NO_SYSTEM;
            }
            AdvertisementSystemData data = initAdvertisementSystemDataMap.get(systemId);
            //如果没有这个系统 报错
            if (data == null){
                return GameErrorCode.E_ADVERTISEMENT_ACTIVITY_NO_SYSTEM;
            }else { //有这个系统 加一个
                userData.getSystemDataMap().put(systemId,data);
                userData.setUpdateOption();
            }
        }
        AdvertisementSystemData systemData = userData.getSystemDataMap().get(systemId);
        if (systemData == null){        //应该不会还没有
            return GameErrorCode.E_ADVERTISEMENT_ACTIVITY_NO_SYSTEM;
        }
        //检测是否可以看广告
        int canWatchCheck = AdvertisementActivityMgr.checkCanWatchAdvertisement(systemData);
        if (canWatchCheck != 0){
            return canWatchCheck;
        }
        //设置当前再看的广告 以及开始时间
        nowWatchAd = systemId;
        nowWatchAdOpenTime = System.currentTimeMillis();
        AdvertisementProto.WatchAdvertisementStartRespMsg.Builder builder = AdvertisementProto.WatchAdvertisementStartRespMsg.newBuilder();
        builder.setRet(0);
        player.sendPacket(ClientProtocol.U_ADVERTISEMENT_ACTIVITY_START_WATCH,builder);

        return 0;
    }

    /**
     * 小玉来看广告游历 特殊处理..
     */
    public AdvertisementProto.WatchAdvertisementOverRespMsg.Builder assistantWatchAdTravel(int activityId,int beautyId){
        AdvertisementProto.WatchAdvertisementOverRespMsg.Builder builder = AdvertisementProto.WatchAdvertisementOverRespMsg.newBuilder();

        int simpleCheck = simpleCheck(activityId);
        if (simpleCheck != 0){
            builder.setRet(simpleCheck);
            return builder;
        }

        AdvertisementConfig config = AdvertisementActivityMgr.getConfig();
        if (config == null){
            builder.setRet(GameErrorCode.E_ADVERTISEMENT_ACTIVITY_NO_OPEN);
            return builder;
        }

        int systemId = eAdvertisementSystemId.travel.getValue();

        if (!userData.getSystemDataMap().containsKey(systemId)){        //如果不存在
            //先去拿总配置 看看有没有这个系统
            Map<Integer, AdvertisementSystemData> initAdvertisementSystemDataMap = AdvertisementActivityMgr.getInitAdvertisementSystemDataMap();
            if (initAdvertisementSystemDataMap == null){
                log.error("广告活动系统数据配置不存在,活动id{}",activityId);
                builder.setRet(GameErrorCode.E_ADVERTISEMENT_ACTIVITY_NO_SYSTEM);
                return builder;
            }
            AdvertisementSystemData initData = initAdvertisementSystemDataMap.get(systemId);
            //如果没有这个系统 报错
            if (initData == null){
                builder.setRet(GameErrorCode.E_ADVERTISEMENT_ACTIVITY_NO_SYSTEM);
                return builder;
            }else { //有这个系统 加一个
                userData.getSystemDataMap().put(systemId,initData);
                userData.setUpdateOption();
            }
        }
        //拿出系统数据
        AdvertisementSystemData data = userData.getSystemDataMap().get(systemId);
        if (data == null){        //如果不存在
            builder.setRet(GameErrorCode.E_ADVERTISEMENT_ACTIVITY_NO_SYSTEM);
            return builder;
        }
        //检测每日次数刷新 检测npc小人
        checkAll();
        //再次检测是否可以看广告
        int canWatchCheck = AdvertisementActivityMgr.checkCanWatchAdvertisement(data);
        if (canWatchCheck != 0){
            builder.setRet(canWatchCheck);
            return builder;
        }
        //拿出配置
        AdvertisementSystemConfig systemConfig = config.getSystemConfigMap().get(systemId);
        if (systemConfig == null){
            builder.setRet(GameErrorCode.E_ADVERTISEMENT_ACTIVITY_NO_SYSTEM);
            return builder;
        }
        //加次数先
        int addWatchTimes = addWatchTimes(data);
        if (addWatchTimes != 0){
            builder.setRet(addWatchTimes);
            return builder;
        }
        String reward = "";

        if (!SystemOpenMgr.systemOpen(player, eSystemId.Travel.getValue())){
            recoveryWatchTimes(data);
            builder.setRet(GameErrorCode.E_SYSTEM_NO_OPEN);
            return builder;
        }

        builder.setRet(0);
        RecoveryRewardProto.RecoverTravelGoRespMsg.Builder travelBuilder = TravelMgr.recoveryTravel(player, StringUtils.string2intSafe(systemConfig.getSpecialReward(), 5),beautyId);
        builder.setTravelResp(travelBuilder);

        //成就活动没过期的话加成就
        if (SystemOpenMgr.systemOpen(player,eSystemId.Advertisement.getValue())){
            if (player.getTitleId() >= config.getTotalWatchMinLevel()){
                if (userData.getActiveTime() + config.getTotalWatchRewardActiveDay() * DateHelper.DAY_MILLIONS >= System.currentTimeMillis()){
                    player.notifyListener(eGamePlayerEventType.TotalWatchAdvertisementTimeCondition.getValue());
                }
            }
        }

        player.notifyListener(eGamePlayerEventType.AdvertisementDailyResetCondition.getValue());

        addTotalWatchTimes(systemId);
        LogWatchAdvertisement log = new LogWatchAdvertisement(activityId,getUserId(),systemId,1,userData.getTotalWatchData(),reward);
        AutoLogMgr.add(log);


        builder.setUserdata(parseUserDataToMsg(userData));
        player.sendPacket(ClientProtocol.U_ADVERTISEMENT_ACTIVITY_WATCH_OVER, builder);

        return builder;


    }


    /**
     * 看广告结束 修改这里的判断的时候记得加上面的游历 策划害的
     * @param activityId
     * @param systemId
     * @param overTime
     * @param isSkip
     * @param param
     * @return
     */

    public AdvertisementProto.WatchAdvertisementOverRespMsg.Builder watchAdOver(int activityId,int systemId,long overTime,int isSkip,int param){

        AdvertisementProto.WatchAdvertisementOverRespMsg.Builder builder = AdvertisementProto.WatchAdvertisementOverRespMsg.newBuilder();

        int simpleCheck = simpleCheck(activityId);
        if (simpleCheck != 0){
            builder.setRet(simpleCheck);
            return builder;
        }
        AdvertisementConfig config = AdvertisementActivityMgr.getConfig();
        if (config == null){
            builder.setRet(GameErrorCode.E_ADVERTISEMENT_ACTIVITY_NO_OPEN);
            return builder;
        }

        if (!userData.getSystemDataMap().containsKey(systemId)){        //如果不存在
            //先去拿总配置 看看有没有这个系统
            Map<Integer, AdvertisementSystemData> initAdvertisementSystemDataMap = AdvertisementActivityMgr.getInitAdvertisementSystemDataMap();
            if (initAdvertisementSystemDataMap == null){
                log.error("广告活动系统数据配置不存在,活动id{}",activityId);
                builder.setRet(GameErrorCode.E_ADVERTISEMENT_ACTIVITY_NO_SYSTEM);
                return builder;
            }
            AdvertisementSystemData initData = initAdvertisementSystemDataMap.get(systemId);
            //如果没有这个系统 报错
            if (initData == null){
                builder.setRet(GameErrorCode.E_ADVERTISEMENT_ACTIVITY_NO_SYSTEM);
                return builder;
            }else { //有这个系统 加一个
                userData.getSystemDataMap().put(systemId,initData);
                userData.setUpdateOption();
            }
        }

        //拿出系统数据
        AdvertisementSystemData data = userData.getSystemDataMap().get(systemId);
        if (data == null){        //如果不存在
            builder.setRet(GameErrorCode.E_ADVERTISEMENT_ACTIVITY_NO_SYSTEM);
            return builder;
        }
        //检测每日次数刷新 检测npc小人
        checkAll();
        //再次检测是否可以看广告
        int canWatchCheck = AdvertisementActivityMgr.checkCanWatchAdvertisement(data);
        if (canWatchCheck != 0){
            builder.setRet(canWatchCheck);
            return builder;
        }

        //拿出配置
        AdvertisementSystemConfig systemConfig = config.getSystemConfigMap().get(systemId);
        if (systemConfig == null){
            builder.setRet(GameErrorCode.E_ADVERTISEMENT_ACTIVITY_NO_SYSTEM);
            return builder;
        }


        //跳过了?
        if (isSkip != 0){
            //拿出渠道配置
            List<String> strings = StringUtils.stringToStringList(ChannelConfig.NEED_WATCH_ADVERSITY_CHANNEL, "\\|");
            //只有配置的子渠道需要看广告 其他都直接走跳过
            if (strings.contains(player.getAppId())){
                int skipType = config.getSkipType();
                //不可跳过
                if (skipType == 0){
                    builder.setRet(GameErrorCode.E_ADVERTISEMENT_ACTIVITY_CAN_NO_SKIP);
                    return builder;
                }else if (skipType == 1){   //月卡
                    if (!player.getModule(CardModule.class).isInCardTime(eCardType.MonthCard.getValue())){
                        builder.setRet(GameErrorCode.E_ADVERTISEMENT_ACTIVITY_CAN_NO_SKIP);
                        return builder;
                    }
                }else if (skipType == 2) {      //年卡
                    if (!player.getModule(CardModule.class).isInCardTime(eCardType.YearCard.getValue())) {
                        builder.setRet(GameErrorCode.E_ADVERTISEMENT_ACTIVITY_CAN_NO_SKIP);
                        return builder;
                    }
                }else {             //其他没加
                    builder.setRet(GameErrorCode.E_ADVERTISEMENT_ACTIVITY_CAN_NO_SKIP);
                    return builder;
                }
            }
        }else {//如果没跳过的话
            //检测是否在看该系统广告
            if (nowWatchAd != systemId){
                builder.setRet(GameErrorCode.E_ADVERTISEMENT_ACTIVITY_NO_SAME_TYPE);
                return builder;
            }
            //柯忠说最短得看6秒 先写死吧
            if ((System.currentTimeMillis() - nowWatchAdOpenTime) < 6 * DateHelper.SECOND_MILLIONS){
                builder.setRet(GameErrorCode.E_ADVERTISEMENT_ACTIVITY_TIME_NO_ENOUGH);
                return builder;
            }
        }

        //加次数先
        int addWatchTimes = addWatchTimes(data);
        if (addWatchTimes != 0){
            builder.setRet(addWatchTimes);
            return builder;
        }



        String reward = "";

        //加奖励
        if (systemId == eAdvertisementSystemId.travel.getValue()){
            if (!SystemOpenMgr.systemOpen(player, eSystemId.Travel.getValue())){
                recoveryWatchTimes(data);
                builder.setRet(GameErrorCode.E_SYSTEM_NO_OPEN);
                return builder;
            }

//            //配置5次 后面改
//            builder.setRet(0);
//            RecoveryRewardProto.RecoverTravelGoRespMsg.Builder travelBuilder = TravelMgr.recoveryTravel(player, StringUtils.string2intSafe(systemConfig.getSpecialReward(),5));
//            builder.setTravelResp(travelBuilder);

            //看广告游历，需要手动处理事件。
            int times = StringUtils.string2intSafe(systemConfig.getSpecialReward(),5);//游历次数
            //游历
            TravelProto.TravelGoRespMsg.Builder respMsg = player.getModule(TravelModule.class).watchAdTravel(times);
            //返回PB
            builder.setRet(0);
            builder.setNewTravelResp(respMsg);
        } else if (systemId == eAdvertisementSystemId.kowloonFishReward.getValue()){
            //没解锁的
            if (!SystemOpenMgr.systemOpen(player, eSystemId.Kowloon.getValue())){
                recoveryWatchTimes(data);
                builder.setRet(GameErrorCode.E_SYSTEM_NO_OPEN);
                return builder;
            }
            String advertisementReward = player.getModule(KowloonModule.class).getAdvertisementReward();
            if (StringUtils.isNullOrEmpty(advertisementReward)){
                recoveryWatchTimes(data);
                builder.setRet(GameErrorCode.E_KOWLOON_CONFIG_NOT_FOUND);
                return builder;
            }
            reward = advertisementReward;
            builder.setRet(0);
            builder.setNormalReward(advertisementReward);
        } else if (systemId == eAdvertisementSystemId.ingotMall.getValue()){

            if (param == 0){
                recoveryWatchTimes(data);
                builder.setRet(GameErrorCode.E_ADVERTISEMENT_ACTIVITY_PARAM_ERROR);
                return builder;

            }
            MallInfo activityMallInfo = MallActivityMgr.getMallInfo(param);
            if (activityMallInfo == null){
                recoveryWatchTimes(data);
                builder.setRet(GameErrorCode.E_MALL_NO_EXIST);
                return builder;
            }
            //配的商店类型就有问题
            if (systemConfig.getShopId() <= 0){
                recoveryWatchTimes(data);
                builder.setRet(GameErrorCode.E_ADVERTISEMENT_ACTIVITY_NO_UNLOCK);
                return builder;
            }
            //不是这个商店类型的
            if (activityMallInfo.getShopType() != eShopType.AdvertisementFreeMall.getValue()){
                recoveryWatchTimes(data);
                builder.setRet(GameErrorCode.E_MALL_NO_EXIST);
                return builder;
            }
            BuyGoodsResult buyGoodsResult = player.getModule(ActivityMallModule.class).buyGoods(activityMallInfo, 1);
            if (buyGoodsResult.getRet() != 0){
                recoveryWatchTimes(data);
                builder.setRet(buyGoodsResult.getRet());
                return builder;
            }
            reward = buyGoodsResult.getReward();
            builder.setRet(0);
            builder.setNormalReward(buyGoodsResult.getReward());
        } else if (systemId == eAdvertisementSystemId.child.getValue()){
            //没解锁的
            if (!SystemOpenMgr.systemOpen(player, eSystemId.Child.getValue())){
                recoveryWatchTimes(data);
                builder.setRet(GameErrorCode.E_SYSTEM_NO_OPEN);
                return builder;
            }
            Property property = PropertyHelper.parseStringToProperty(systemConfig.getReward());
            player.getModule(CurrencyModule.class).addCurrency(property, eLogMoneyType.Advertisement,eLogMoneyType.AdvertisementGet);

            builder.setRet(0);

            reward = systemConfig.getReward();
            builder.setNormalReward(systemConfig.getReward());
        } else if (systemId == eAdvertisementSystemId.buildingNpc.getValue()){
            //先取出来
            String npcReward = userData.getNpcReward();
            if (StringUtils.isNullOrEmpty(npcReward)){
                recoveryWatchTimes(data);
                builder.setRet(GameErrorCode.E_ADVERTISEMENT_NPC_DATA_ERROR);
                return builder;
            }
            //直接刷一个新的奖励
            userData.setNpcReward(AdvertisementActivityMgr.randomNpcReward());

            Property property = PropertyHelper.parseStringToProperty(npcReward);
            player.getModule(CurrencyModule.class).addCurrency(property, eLogMoneyType.Advertisement,eLogMoneyType.AdvertisementGet);
            builder.setRet(0);
            builder.setNormalReward(npcReward);
            reward = npcReward;
        } else if (systemId == eAdvertisementSystemId.lottery.getValue()){
            //没解锁的
            if (!SystemOpenMgr.systemOpen(player, eSystemId.TurntableLottery.getValue())){
                recoveryWatchTimes(data);
                builder.setRet(GameErrorCode.E_SYSTEM_NO_OPEN);
                return builder;

            }
            int times = StringUtils.string2intSafe(systemConfig.getSpecialReward(), 10);
            List<Property> propertyList = new ArrayList<>();
            propertyList = player.getModule(PlayerModule.class).doTurntableLottery(3,times,new Property());
            List<String> propertyStringList = new ArrayList<>();

            Property totalReward = new Property();
            for (Property property :propertyList){
                totalReward.addProperty(property);
                propertyStringList.add(PropertyHelper.parsePropertyToString(property));
            }

//            player.getModule(CurrencyModule.class).addCurrency(totalReward, eLogMoneyType.Advertisement,eLogMoneyType.AdvertisementGet);
            builder.setRet(0);
            builder.addAllLotteryReward(propertyStringList);
            reward = systemConfig.getReward();
        } else if (systemId == eAdvertisementSystemId.washe.getValue()){
            //没解锁的
            if (!SystemOpenMgr.systemOpen(player, eSystemId.WasheActivity.getValue())){
                recoveryWatchTimes(data);
                builder.setRet(GameErrorCode.E_SYSTEM_NO_OPEN);
                return builder;
            }
            WasheConfig washeConfig = WasheMgr.getWasheConfig();
            if (washeConfig == null){
                recoveryWatchTimes(data);
                builder.setRet(GameErrorCode.E_WASHE_WEIHU);
                return builder;
            }

            Property property = PropertyHelper.parseStringToProperty(systemConfig.getReward());
            player.getModule(CurrencyModule.class).addCurrency(property, eLogMoneyType.Advertisement,eLogMoneyType.AdvertisementGet);
            builder.setRet(0);
            builder.setNormalReward(systemConfig.getReward());
            reward = systemConfig.getReward();
        }else if(systemId == eAdvertisementSystemId.kowloonPlayTime.getValue()){
            //没解锁的
            if (!SystemOpenMgr.systemOpen(player, eSystemId.Kowloon.getValue())){
                recoveryWatchTimes(data);
                builder.setRet(GameErrorCode.E_SYSTEM_NO_OPEN);
                return builder;
            }

            int ret = player.getModule(KowloonModule.class).addKowloonTime(StringUtils.string2intSafe(systemConfig.getSpecialReward(), 1));
            if (ret!=0){
                recoveryWatchTimes(data);
                builder.setRet(ret);
                return builder;
            }
            builder.setRet(0);
            builder.setNormalReward("");

        } else if (systemId == eAdvertisementSystemId.addEarnSpeed.getValue()){
            int ret = addEarnSpeedAdditionTime();
            if (ret!=0){
                recoveryWatchTimes(data);
                builder.setRet(ret);
                return builder;
            }
            String specialReward = systemConfig.getSpecialReward();
            List<Integer> integers = StringUtils.stringToIntegerList(specialReward, "\\|");
            if (integers.size()!=3){
                recoveryWatchTimes(data);
                builder.setRet(GameErrorCode.E_ADVERTISEMENT_ACTIVITY_PARAM_ERROR);
                return builder;
            }

            BigInteger earnSpeed = player.getModule(EarnSpeedModule.class).getEarnSpeed();
            BigInteger addValue = earnSpeed.multiply(BigInteger.valueOf(integers.get(2) * DateHelper.HOUR_SECONDS));

            Property sliverReward = new Property(GameConfig.GAME_MONEY_SILVER, addValue);
            player.getModule(CurrencyModule.class).addCurrency(sliverReward,
                    eLogMoneyType.Advertisement,eLogMoneyType.AdvertisementGet);
            reward = PropertyHelper.parsePropertyToString(sliverReward);
            builder.setRet(0);
            builder.setNormalReward(PropertyHelper.parsePropertyToString(sliverReward));
        } else if (systemId == eAdvertisementSystemId.specialCookBoyFirst.getValue()){
            int ret = player.getModule(SpecialCookBoyModule.class).watchAdGetTipReward(1,param);
            if (ret!=0){
                recoveryWatchTimes(data);
                builder.setRet(ret);
                return builder;
            }
            reward = param+"=1";
            builder.setRet(0);
            builder.setNormalReward("");

        } else if (systemId == eAdvertisementSystemId.specialCookBoySecond.getValue()){
            int ret = player.getModule(SpecialCookBoyModule.class).watchAdGetTipReward(2,param);
            if (ret != 0){
                recoveryWatchTimes(data);
                builder.setRet(ret);
                return builder;
            }
            reward = param+"=1";
            builder.setRet(0);
            builder.setNormalReward("");

        }
        else if (systemId == eAdvertisementSystemId.unionIntermediateConstruct.getValue()){
            //没解锁的
            if (!SystemOpenMgr.systemOpen(player, eSystemId.Union.getValue())){
                recoveryWatchTimes(data);
                builder.setRet(GameErrorCode.E_SYSTEM_NO_OPEN);
                return builder;
            }
            String specialReward = systemConfig.getSpecialReward();
            int time = StringUtils.string2intSafe(specialReward, 1);
            if (player.getModule(CardModule.class).isUnionConstructMidFree()){
                builder.setRet(GameErrorCode.E_ADVERTISEMENT_ACTIVITY_HAS_CARD_TO_CONSTRUCT);
                return builder;
            }

            player.getModule(CrossUnionModule.class).freeConstructUnion(2);

            builder.setRet(0);
            builder.setNormalReward("");

        } else if (systemId == eAdvertisementSystemId.unionSeniorConstruct.getValue()){
            //没解锁的
            if (!SystemOpenMgr.systemOpen(player, eSystemId.Union.getValue())){
                recoveryWatchTimes(data);
                builder.setRet(GameErrorCode.E_SYSTEM_NO_OPEN);
                return builder;
            }
            String specialReward = systemConfig.getSpecialReward();
            int time = StringUtils.string2intSafe(specialReward, 1);
            if (player.getModule(CardModule.class).isUnionConstructSupFree()){
                builder.setRet(GameErrorCode.E_ADVERTISEMENT_ACTIVITY_HAS_CARD_TO_CONSTRUCT);
                return builder;
            }

            player.getModule(CrossUnionModule.class).freeConstructUnion(3);

            builder.setRet(0);
            builder.setNormalReward("");
        } else if (systemId == eAdvertisementSystemId.tombRobChooseSkill.getValue()){
            //盗墓奇遇看广告全选技能
            try {
                player.getModule(DaoMuActivityModule.class).doAddSkillSelectAllTime(param);
                builder.setRet(0);
            } catch (BusinessException e) {
                log.error("盗墓全选技能异常：{}", e.getCode());
                recoveryWatchTimes(data);
                builder.setRet(e.getCode());
                return builder;
            }
        } else if (systemId == eAdvertisementSystemId.tombRobRevive.getValue()){
            //盗墓奇遇看广告复活
            try {
                DaoMuProto.DaoMuPveRoomPlayer daoMuPveRoomPlayer = player.getModule(DaoMuActivityModule.class).doResurgence(param);
                builder.setRet(0);
                builder.setDaoMuPveRoomPlayer(daoMuPveRoomPlayer);
            } catch (BusinessException e) {
                log.error("盗墓复活异常：{}", e.getCode());
                recoveryWatchTimes(data);
                builder.setRet(e.getCode());
                return builder;
            }
        }   else if (systemId == eAdvertisementSystemId.LuckyShop.getValue()) {
            //没解锁的
//            if (!SystemOpenMgr.systemOpen(player, eSystemId.LuckyShop.getValue())) {
//                recoveryWatchTimes(data);
//                return GameErrorCode.E_SYSTEM_NO_OPEN;
//            }
            LuckyShopModule luckyShopModule = player.getModule(LuckyShopModule.class);
            int rewardId = luckyShopModule.draw();
            if (rewardId != 0) {
                // 获得奖励
                LuckyShopConfig luckyShopConfig = LuckyShopMgr.getConfig();
                if (luckyShopConfig != null) {
                    LuckyShopDrawConfig drawConfig = luckyShopConfig.getLuckyShopDrawConfig(rewardId);
                    if (drawConfig != null) {
                        reward = drawConfig.getReward();
                        player.getModule(CurrencyModule.class).addCurrency(reward, eLogMoneyType.Advertisement,eLogMoneyType.AdvertisementGet);
                        AutoLogMgr.add(new LogLuckyShopDraw(player.getUserId(), luckyShopConfig.getActivityId(), reward));
                    }
                }
            }
            builder.setRet(0);
            builder.setNormalReward(reward);
            builder.setExtraParam(String.valueOf(rewardId));
            builder.setLuckyShopUserData(luckyShopModule.parseLuckyShopUserDataMsg());
        } else if(systemId == eAdvertisementSystemId.Login.getValue()){
            reward = systemConfig.getReward();
            builder.setRet(0);
            builder.setNormalReward(reward);
            player.getModule(CurrencyModule.class).addCurrency(reward, eLogMoneyType.Advertisement,eLogMoneyType.AdvertisementGet);
        } else if(systemId == eAdvertisementSystemId.SevenSign.getValue()){
            //没解锁的
            if (!SystemOpenMgr.systemOpen(player, eSystemId.DaySign.getValue())){
                recoveryWatchTimes(data);
                builder.setRet(GameErrorCode.E_SYSTEM_NO_OPEN);
                return builder;
            }
            reward = systemConfig.getReward();
            builder.setRet(0);
            builder.setNormalReward(reward);
            player.getModule(CurrencyModule.class).addCurrency(reward, eLogMoneyType.Advertisement,eLogMoneyType.AdvertisementGet);
        } else { //没这个系统 应该不会进来
            log.error("广告领奖 没有这个系统id{}", systemId);
            recoveryWatchTimes(data);
            LogWatchAdvertisement log = new LogWatchAdvertisement(activityId,getUserId(),systemId,0,userData.getTotalWatchData(),reward);
            AutoLogMgr.add(log);
            builder.setRet(GameErrorCode.E_ADVERTISEMENT_ACTIVITY_NO_SYSTEM);
            return builder;
        }

        //成就活动没过期的话加成就
        if (SystemOpenMgr.systemOpen(player,eSystemId.Advertisement.getValue())){
            if (player.getTitleId() >= config.getTotalWatchMinLevel()){
                if (userData.getActiveTime() + config.getTotalWatchRewardActiveDay() * DateHelper.DAY_MILLIONS >= System.currentTimeMillis()){
                    player.notifyListener(eGamePlayerEventType.TotalWatchAdvertisementTimeCondition.getValue());
                }
            }
        }

        player.notifyListener(eGamePlayerEventType.AdvertisementDailyResetCondition.getValue());

        addTotalWatchTimes(systemId);
        LogWatchAdvertisement log = new LogWatchAdvertisement(activityId,getUserId(),systemId,1,userData.getTotalWatchData(),reward);
        AutoLogMgr.add(log);


        builder.setUserdata(parseUserDataToMsg(userData));
        player.sendPacket(ClientProtocol.U_ADVERTISEMENT_ACTIVITY_WATCH_OVER, builder);

        return builder;

    }

    /**
     * 加总次数
     * @param systemId
     */

    public void addTotalWatchTimes(int systemId){
        if (userData == null){
            return;
        }
        Map<Integer, Integer> totalWatchData = userData.getTotalWatchData();
        if (totalWatchData == null){
            userData.setTotalWatchData(new ConcurrentHashMap<>());
            totalWatchData = userData.getTotalWatchData();
        }
        if (totalWatchData.containsKey(systemId)){
            totalWatchData.put(systemId, totalWatchData.get(systemId) + 1);
        }else {
            totalWatchData.put(systemId, 1);
        }
        userData.setUpdateOption();
    }

    /**
     * 加次数
     * @param systemData
     * @return
     */

    public int addWatchTimes(AdvertisementSystemData systemData){
        if (systemData == null){
            return GameErrorCode.E_ADVERTISEMENT_ACTIVITY_NO_SYSTEM;
        }
        systemData.setHasWatchTimes(systemData.getHasWatchTimes()+1);
        systemData.setLastWatchTime(System.currentTimeMillis());
        userData.setUpdateOption();
        syncData();
        return 0;
    }

    public int dailyWatchTimes(){
        if(userData == null){
            return 0;
        }
        int total = 0;
        for (Map.Entry<Integer, AdvertisementSystemData> entry : userData.getSystemDataMap().entrySet()) {
            total += entry.getValue().getHasWatchTimes();
        }
        return total;
    }

    /**
     * 恢复多扣的次数
     * @return
     */
    public int recoveryWatchTimes(AdvertisementSystemData systemData){

        if (systemData == null){        //如果不存在
            return GameErrorCode.E_ADVERTISEMENT_ACTIVITY_NO_SYSTEM;
        }
        //扣已看次数
        if (systemData.getHasWatchTimes() > 0){
            systemData.setHasWatchTimes(systemData.getHasWatchTimes()-1);
            userData.setUpdateOption();
            syncData();
        }
        return 0;
    }



    public int simpleCheck(int activityId){
        ActivityInfo activityInfo = AdvertisementActivityMgr.getActivityInfo();
        if (activityInfo == null){
            return GameErrorCode.E_ADVERTISEMENT_ACTIVITY_NO_OPEN;
        }
        if (activityInfo.getActivityId() != activityId){
            return GameErrorCode.E_ADVERTISEMENT_ACTIVITY_NO_OPEN;
        }
        // 没有活动
        if (!ActivityMgr.activityInTime(AdvertisementActivityMgr.getActivityInfo()) || AdvertisementActivityMgr.getConfig() == null) {
            return GameErrorCode.E_ADVERTISEMENT_ACTIVITY_NO_OPEN;
        }
        if (userData == null || userData.getActivityId()!=activityId){
            initUserData(activityId);
        }
        if (userData == null){
            return GameErrorCode.E_ADVERTISEMENT_ACTIVITY_NO_OPEN;
        }
        return 0;
    }

    /**
     * 检测刷新npc商店 6.0弃用
     */
    public void checkRefreshNpcMall(){
        ActivityInfo activityInfo = AdvertisementActivityMgr.getActivityInfo();
        if (activityInfo == null){
            return;
        }
        AdvertisementConfig config = AdvertisementActivityMgr.getConfig();
        if (config == null){
            return;
        }
        if (userData == null){
            initUserData(activityInfo.getActivityId());
            if (userData == null){
                return;
            }
        }
        List<Long> time = AdvertisementActivityMgr.getNpcStageAndRefreshTime();
        if (time == null){
            return;
        }
        if (time.size() != 2){
            return;
        }
        //如果没初始化过 || 相同阶段 但是不同天 || 不同阶段
        if (StringUtils.isNullOrEmpty(userData.getNpcReward()) || (userData.getNpcDailyStage() == time.get(0) && userData.getNpcLastRefreshTime() != time.get(1)) || userData.getNpcDailyStage() != time.get(0)){
            String npcReward = AdvertisementActivityMgr.randomNpcReward();
            if (npcReward == null){
                return;
            }
            //设值
            userData.setNpcDailyStage(time.get(0));
            userData.setNpcLastRefreshTime(time.get(1));
            //刷新奖励
            userData.setNpcReward(npcReward);
            //刷新次数
            AdvertisementSystemData data = userData.getSystemDataMap().get(eAdvertisementSystemId.buildingNpc.getValue());
            if (data != null){
                data.setHasWatchTimes(0);
                data.setLastRefreshLimitTime(System.currentTimeMillis());
            }else {
                log.error("广告活动刷新npc商店时，玩家数据{}没有找到对应的系统id",getUserId());
            }
            userData.setUpdateOption();
            syncData();
        }
    }

    /**
     * 增加赚速加成时间
     */
    public int addEarnSpeedAdditionTime(){
        if (AdvertisementActivityMgr.getActivityInfo() == null){
            return GameErrorCode.E_ADVERTISEMENT_ACTIVITY_NO_OPEN;
        }
        AdvertisementConfig config = AdvertisementActivityMgr.getConfig();
        if (config == null){
            return GameErrorCode.E_ADVERTISEMENT_ACTIVITY_NO_OPEN;
        }
        if (userData == null){
            return GameErrorCode.E_ADVERTISEMENT_ACTIVITY_NO_OPEN;
        }
        if(userData.getEarnSpeedBuffCount() == 0){
            userData.setEarnSpeedBuffCount(1);
        }
        userData.setEarnSpeedBuffRefreshTime(System.currentTimeMillis());
        userData.setUpdateOption();
        syncData();
        return 0;
    }


    /**
     * 检测刷新赚速加成
     */
    public void refreshEarnSpeedAddition(int additionTime){
        if (AdvertisementActivityMgr.getActivityInfo() == null){
            return;
        }
        AdvertisementConfig config = AdvertisementActivityMgr.getConfig();
        if (config == null){
            return;
        }
        if (userData == null){
            return;
        }
        //如果过期了
        if (userData.getEarnSpeedBuffRefreshTime() + additionTime * DateHelper.HOUR_MILLIONS  < System.currentTimeMillis()){
            userData.setEarnSpeedBuffCount(0);
        }
    }

    /**
     * 获取当前赚速加成
     * @return
     */
    public int getEarnSpeedAddition(){
        if (AdvertisementActivityMgr.getActivityInfo() == null){
            return 0;
        }
        AdvertisementConfig config = AdvertisementActivityMgr.getConfig();
        if (config == null){
            return 0;
        }
        if (userData == null){
            return 0;
        }
        //每次增加的加成
        int additionPerTime = 0;
        //加成时间
        int additionTime = 0;
        AdvertisementSystemConfig systemConfig = config.getSystemConfigMap().get(eAdvertisementSystemId.addEarnSpeed.getValue());
        if (systemConfig != null){
            String specialReward = systemConfig.getSpecialReward();
            List<Integer> integers = StringUtils.stringToIntegerList(specialReward, "\\|");
            if (integers.size() == 3){
                additionPerTime = integers.get(1);
                additionTime = integers.get(0);
            }
        }

        refreshEarnSpeedAddition(additionTime);
        //暂时只会有一次加成
        return userData.getEarnSpeedBuffCount() * additionPerTime;

    }

    /**
     * 触发在线时长礼包
     * @param onlineTime 在线时长
     */
    public void touchOnlineTimeAd(long onlineTime){
        if(userData == null){
            return;
        }
        //已经看过广告就跳过
        int dailyWatchTimes = dailyWatchTimes();
        if(dailyWatchTimes > 0){
            return;
        }
        Map<Integer, AdvertisementSystemData> initAdvertisementSystemDataMap = AdvertisementActivityMgr.getInitAdvertisementSystemDataMap();
        if (initAdvertisementSystemDataMap == null){
            return;
        }
        int systemId = eAdvertisementSystemId.Login.getValue();
        AdvertisementSystemData data = userData.getSystemDataMap().get(systemId);
        if (data == null){
            data = initAdvertisementSystemDataMap.get(systemId);
            if (data == null) {
                return;
            }
            data.setLastRefreshLimitTime(DateHelper.getTodayZeroTimeStamp());
            userData.getSystemDataMap().put(systemId, data);
            userData.setUpdateOption();
        }
        long zeroTimeStamp = DateHelper.getTodayZeroTimeStamp();
        //还没重置
        if(data.getLastRefreshLimitTime() < zeroTimeStamp){
            return;
        }
        //已经触发
        if(data.getLastRefreshLimitTime() > zeroTimeStamp){
            return;
        }
        AdvertisementConfig config = AdvertisementActivityMgr.getConfig();
        if(config == null){
            return;
        }
        if(config.getLoginTriggerCond() <= 0 || config.getExistTime() <= 0){
            return;
        }
        //触发条件不足
        if(onlineTime * DateHelper.MINUTE_SECONDS < config.getLoginTriggerCond()){
            return;
        }
        long nowTime = System.currentTimeMillis();
        //触发过，还没过期
        if(config.getExistTime() * DateHelper.SECOND_MILLIONS + data.getLastRefreshLimitTime() < nowTime  && data.getLastRefreshLimitTime() > zeroTimeStamp){
            return;
        }
        //触发过，未看过广告
        if(data.getLastWatchTime() > 0 && data.getLastWatchTime() < data.getLastRefreshLimitTime() && data.getLastRefreshLimitTime() > zeroTimeStamp){
            return;
        }
        AdvertisementSystemConfig systemConfig = config.getSystemConfigMap().get(systemId);
        //领奖次数到了，不再触发
        if(data.getHasWatchTimes() >= systemConfig.getNumMax()){
            return;
        }
        data.setLastRefreshLimitTime(nowTime);
        userData.setUpdateOption();
        syncData();
    }

    /**
     * 触发七日签到广告
     */
    public void touchSevenSignAd(){
        if(userData == null){
            return;
        }
        AdvertisementConfig config = AdvertisementActivityMgr.getConfig();
        if(config == null){
            return;
        }
        AdvertisementSystemConfig systemConfig = config.getSystemConfigMap().get(eAdvertisementSystemId.SevenSign.getValue());
        if(systemConfig == null){
            return;
        }
        long serverId = ConfigHelper.getLong("serverId");

        int serverOpenDay = ServerListMgr.getServerOpenDay(serverId);
        //开服天数限制
        if(serverOpenDay > systemConfig.getButtonFinish()){
            return;
        }
        Map<Integer, AdvertisementSystemData> initAdvertisementSystemDataMap = AdvertisementActivityMgr.getInitAdvertisementSystemDataMap();
        if (initAdvertisementSystemDataMap == null){
            return;
        }
        int systemId = eAdvertisementSystemId.SevenSign.getValue();

        long zeroTimeStamp = DateHelper.getTodayZeroTimeStamp();
        AdvertisementSystemData data = userData.getSystemDataMap().get(systemId);
        if (data == null){
            data = initAdvertisementSystemDataMap.get(systemId);
            data.setLastRefreshLimitTime(zeroTimeStamp);
            userData.getSystemDataMap().put(systemId, data);
            userData.setUpdateOption();
        }
        //还没重置
        if(data.getLastRefreshLimitTime() < zeroTimeStamp){
            return;
        }
        //已经触发
        if(data.getLastRefreshLimitTime() > zeroTimeStamp){
            return;
        }

        long nowTime = System.currentTimeMillis();
        //触发过，还没过期
        if(config.getExistTime() * DateHelper.SECOND_MILLIONS + data.getLastRefreshLimitTime() < nowTime && data.getLastRefreshLimitTime() > zeroTimeStamp){
            return;
        }
        //未看过广告
        if(data.getLastWatchTime() > 0 && data.getLastWatchTime() < data.getLastRefreshLimitTime() && data.getLastRefreshLimitTime() > zeroTimeStamp){
            return;
        }

        //领奖次数到了，不再触发
        if(data.getHasWatchTimes() >= systemConfig.getNumMax()){
            return;
        }
        data.setLastRefreshLimitTime(nowTime);
        userData.setUpdateOption();
        syncData();
    }
}


